/**
 * 
 */
package neptune.web.server.operations;

import it.robertobifulco.ias.IncorrectParamsException;
import it.robertobifulco.ias.Operation;
import neptune.manager.NeptuneManager;
import neptune.manager.model.topology.Topology;
import neptune.manager.storage.XMLTopologyStorage;
import neptune.manager.validator.ValidatorResult;
import neptune.web.server.ApplicationManager;

/**
 * @author Vathalas
 * 
 */
public class WriteTopologyFile implements Operation {

	public static String ID = "WriteTopologyFile";

	/*
	 * (non-Javadoc)
	 * 
	 * @see it.robertobifulco.ias.Operation#execute(java.lang.Object[])
	 */
	/**
	 * Questa operazione puo' avere come primo argomento due oggetti differenti: <br>
	 * <br>
	 * E' possibile passare il contenuto di un file di topologia da scrivere
	 * direttamente su file, oppure e' possibile passare un oggetto Topology,
	 * che verra' scritto su file adoperando i metodi di TopologyStorage.
	 * 
	 * @param args
	 *            [0] = String File content | Topology experimentTopology
	 * @param args
	 *            [1] = String expName
	 * @return ValidatorResult
	 *           
	 * 
	 */
	public Object[] execute(Object[] args) throws IncorrectParamsException,
			Exception {

		String expName = (String) args[1];

		NeptuneManager manager = ApplicationManager.getApplicationManager()
				.getNeptuneManager();

		ValidatorResult result;

		if (args[0] instanceof Topology) {
			result = manager
					.saveExperimentTopology(expName, (Topology) args[0]);
		} else {

			String xml = (String) args[0];
			result = manager.saveExperimentTopology(expName, XMLTopologyStorage
					.getTopologyFromXML(xml));
		}

		return new Object[] { result };
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see it.robertobifulco.ias.Operation#getDescription()
	 */
	public String getDescription() {
		return "Operation that writes the topology file for a given experiment";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see it.robertobifulco.ias.Operation#getID()
	 */
	public String getID() {
		return ID;
	}

}
